package com.example.project.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.example.project.entity.Tracker;

/**
 * 跟踪类型Mapper接口
 * 
 * @author ruoyi
 * @date 2025-09-19
 */
@Mapper
public interface TrackerMapper {
	
	/**
	 * 查询跟踪类型
	 * 
	 * @param id 跟踪类型主键
	 * @return 跟踪类型
	 */
	@Select("select id, name, is_in_chlog, icon from t_tracker where del_flag = 0 and id = #{id}")
	public Tracker selectTTrackersById(Long id);

	/**
	 * 查询跟踪类型列表
	 * 
	 * @param tTrackers 跟踪类型
	 * @return 跟踪类型集合
	 */
	@Select("<script>" +
			"select id, name, is_in_chlog, icon from t_tracker where del_flag = 0 " +
			"  <if test='name != null and name != \"\"'> and name like concat('%', #{name}, '%')</if>" +
			"  <if test='isInChlog != null'> and is_in_chlog = #{isInChlog}</if>" +
			"</script>")
	public List<Tracker> selectTTrackersList(Tracker tTrackers);

	/**
	 * 新增跟踪类型
	 * 
	 * @param tTrackers 跟踪类型
	 * @return 结果
	 */
	@Insert("<script>" +
			"insert into t_tracker" +
			"<trim prefix='(' suffix=')' suffixOverrides=','>" +
			"  <if test='name != null and name != \"\"'>name,</if>" +
			"  <if test='isInChlog != null'>is_in_chlog,</if>" +
			"  <if test='icon != null'>icon,</if>" +
			"del_flag," +
			"<if test=\"createBy != null\">create_by,</if>" + 
			"<if test=\"createTime != null\">create_time,</if>" +
			"</trim>" +
			"<trim prefix='values (' suffix=')' suffixOverrides=','>" +
			"  <if test='name != null and name != \"\"'>#{name},</if>" +
			"  <if test='isInChlog != null'>#{isInChlog},</if>" +
			"  <if test='icon != null'>#{icon},</if>" +
			"0," +
			"<if test=\"createBy != null\">#{createBy},</if>" + 
			"<if test=\"createTime != null\">#{createTime},</if>" +
			"</trim>" +
			"</script>")
	public int insertTTrackers(Tracker tTrackers);

	/**
	 * 修改跟踪类型
	 * 
	 * @param tTrackers 跟踪类型
	 * @return 结果
	 */
	@Update("<script>" +
			"update t_tracker" +
			"<trim prefix='SET' suffixOverrides=','>" +
			"  <if test='name != null and name != \"\"'>name = #{name},</if>" +
			"  <if test='isInChlog != null'>is_in_chlog = #{isInChlog},</if>" +
			"  <if test='icon != null'>icon = #{icon},</if>" +
			"<if test=\"updateBy != null\">update_by = #{updateBy},</if>" +
			"<if test=\"updateTime != null\">update_time = #{updateTime},</if>" +
			"</trim>" +
			"where id = #{id}" +
			"</script>")
	public int updateTTrackers(Tracker tTrackers);

	/**
	 * 删除跟踪类型
	 * 
	 * @param id 跟踪类型主键
	 * @return 结果
	 */
//	@Delete("delete from t_tracker where id = #{id}")
	@Update("update t_tracker set del_flag = 1, update_time = NOW() where id = #{id}")
	public int deleteTTrackersById(Long id);

	/**
	 * 批量删除跟踪类型
	 * 
	 * @param ids 需要删除的数据主键集合
	 * @return 结果
	 */
//	@Delete("<script>" +
//			"delete from t_tracker where id in " +
//			"<foreach item='id' collection='array' open='(' separator=',' close=')'>" +
//			"  #{id}" +
//			"</foreach>" +
//			"</script>")
	@Update("<script>" + "update t_tracker set del_flag = 1, update_time = NOW() where id in "
			+ "<foreach item='id' collection='array' open='(' separator=',' close=')'>" + "#{id}" + "</foreach>"
			+ "</script>")
	public int deleteTTrackersByIds(@Param("array") String[] ids);

	@Select("select id, name, is_in_chlog, icon from t_tracker where del_flag = 0")
	public List<Tracker> selectTrackersAll();

	/**
	 * 将所有跟踪器的is_in_chlog设置为0
	 * @return 结果
	 */
	@Update("update t_tracker set is_in_chlog = 0")
	public int updateAllIsInChlogToZero();

	/**
	 * 根据ID更新跟踪器的is_in_chlog字段
	 * @param id 跟踪器ID
	 * @param isInChlog 是否显示在变更日志中
	 * @return 结果
	 */
	@Update("update t_tracker set is_in_chlog = #{isInChlog} where id = #{id}")
	public int updateIsInChlogById(@Param("id") Long id, @Param("isInChlog") Integer isInChlog);
}
